package 力扣日常刷题.木22二月.第14天220;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Stack;

/**
 * @author 帅小伙
 * @date 2022/2/20
 * @description
 */
public class Demo111字符串解码2 {

    public String decodeString(String s) {
        int multi = 0;
        StringBuilder res = new StringBuilder();

        Stack<Integer> stackMulti = new Stack();
        Stack<StringBuilder> stackRes = new Stack();

        for (char c : s.toCharArray()) {
            if(c == '[') {
                stackMulti.push(multi);
                stackRes.push(res);
                multi  = 0;
                res = new StringBuilder();
            }else if(c == ']'){
                Integer pop = stackMulti.pop();
                StringBuilder t = new StringBuilder();
                for (int i = 0; i < pop; i++) t.append(res);
                res = new StringBuilder(stackRes.pop()).append(t);
            }else if(c >='0' && c <='9') {
                multi = multi * 10 + (c - '0');
            }else res.append(c);
        }
        return res.toString();

    }

}
